package controllayer;

import java.util.ArrayList;
import java.util.Date;

import modellayer.Employee;
import modellayer.Sale;
import dblayer.DBConnection;
import dblayer.DBSale;
import dblayer.IFDBSale;

public class CtrSale {

	private IFDBSale dbObj;
	
	public CtrSale(){
		dbObj = new DBSale();
	}
	
	public ArrayList<Sale> getAllSales(){
		ArrayList<Sale> list = new ArrayList<Sale>();
		list = dbObj.getAllSales();
		return list;
	}
	
	public Sale searchByID(int id){
		return dbObj.searchByID(id);
	}
	
	//
	
	public int updateSale(int id, Date date, double total, boolean status, Employee employee){
		Sale sale = new Sale();
		
		sale.setID(id);
		sale.setDate(date);
		sale.setTotal(total);
		sale.setStatus(status);
		sale.setEmployer(employee);
		
		return dbObj.updateSale(sale);
	}
	
	public void createSale(int id, Date date, double total, boolean status, Employee employee){
		Sale sale = new Sale();
		
		sale.setID(id);
		sale.setDate(date);
		sale.setTotal(total);
		sale.setStatus(status);
		sale.setEmployer(employee);
		
		try{
			DBConnection.startTransaction();
			dbObj.insertSale(sale);
			DBConnection.commitTransaction();
		}
		catch(Exception e){
			DBConnection.rollbackTransaction();
		}
	}
	
	public int deleteSale(int id){
		return dbObj.deleteSale(id);
	}
}
